home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / C / CSOURCE.ZIP / WARP.C < prev   
Encoding:
C/C++ Source or Header  |  1994-07-07  |  6.1 KB  |  211 lines

  1. /****************************************************************
  2. * FILE:    warp.c
  3. * DESC:    This program lets you specify a file to warp, then
  4. *        prompts you for control lines. It uses the lines to
  5. *        warp the underlying image, going a step at a time,
  6. *        tweening to the target and optionally saving the
  7. *        sequence as numbered PCX files.
  8. * HISTORY:    Created     7/06/1994
  9. * LAST CHANGED: 7/06/1994
  10. *    Copyright (c) 1994 by Scott Anderson
  11. *
  12. ****************************************************************/
  13.  
  14. /* ----------------------INCLUDES----------------------------- */
  15.  
  16. #include <conio.h>
  17. #include <stdio.h>
  18. #include <io.h>
  19. #include <dos.h>            /* for the mouse */
  20. #include <math.h>
  21. #include <graph.h>
  22. #include <memory.h>
  23. #include <string.h>
  24.  
  25. #include "define.h"
  26.  
  27. /* ----------------------DEFINES------------------------------ */
  28.  
  29. #define WARP_TWEENS        2
  30.  
  31. /* ----------------------PROTOTYPES--------------------------- */
  32.  
  33. int        tweenWarp(PICTURE *src);
  34.  
  35. /* ----------------------EXTERNALS---------------------------- */
  36.  
  37. /**** line routines ****/
  38. extern void        editLines(PICTURE *pic, LINE_LIST *lineList);
  39. extern void        createLines(PICTURE *pic, LINE_LIST *lineList);
  40.  
  41. /**** warping and morphing routines ****/
  42. extern int        sumLines(PICTURE *picture, COLOR *color,
  43.                     LINE *origline, POINT *warp, LINE *warpline);
  44. extern int        setLength(LINE *line);
  45.  
  46. /**** i/o routines ****/
  47. extern LINE_LIST *loadLines(char *filename, char *extension);
  48. extern void      saveLines(char *filename,
  49.                         LINE_LIST *lineList, char *extension);
  50.  
  51. /**** external variables ****/ 
  52. extern char        *OutFilename;
  53. /* set from last picture loaded */
  54. extern int        Xmin, Ymin, Xmax, Ymax;
  55.  
  56. /* ----------------------GLOBAL DATA-------------------------- */
  57.  
  58. PICTURE *Src;            /* source picture pointer */
  59.  
  60. LINE SrcLine[MAX_LINES];
  61. LINE DstLine[MAX_LINES];
  62.  
  63. int        Tweens;
  64. int        StartLine;
  65. int        NumLines;
  66.  
  67. /*****************************************************************
  68. * FUNC: main (int argc, char *argv[])
  69. * DESC: Read in a filename to warp
  70. *****************************************************************/
  71.  
  72. main (int argc, char *argv[])
  73. {
  74.     int        segment;
  75.     LINE_LIST *lineSrcList;
  76.     LINE_LIST *lineDstList;
  77.     char    answer;
  78.  
  79.     /* load the pcx file if one is given */
  80.     if ((2 > argc) || (argc > 4)) {
  81.         printf("Usage: warp <file> [<steps> [<output>]]\n\n");
  82.         printf("Where: <file>   is the source PCX filename\n");
  83.         printf("       <steps>  is the optional sequence size\n");
  84.         printf("                (the max is %d, the default is %d)\n",
  85.                                    MAX_TWEENS, WARP_TWEENS);
  86.         printf("       <output> is the optional output filename\n");
  87.         printf("                (defaults to no output)\n\n");
  88.         printf("Note:  The output filename can be at most %d characters long.\n",
  89.                                 MAX_NAME_SIZE);
  90.         printf("       The PCX extension is added automatically, so don't\n");
  91.         printf("       include it in the filename.\n");
  92.         printf("       Morph only accepts PCX files with %d X %d resolution\n",
  93.                                 MAX_WIDE, MAX_TALL);
  94.         printf("       and %d colors.\n", COLORS);
  95.         exit(0);
  96.     }
  97.     if (argc > 2) {
  98.         Tweens = clip (atoi(argv[2]), 1, MAX_TWEENS);
  99.         if (argc > 3)
  100.             OutFilename = argv[3];
  101.     }
  102.     else
  103.         Tweens = WARP_TWEENS;
  104.  
  105.     printf("Loading the file %s\n", argv[1]);
  106.     Src = loadPicture(argv[1]);
  107.     if (Src == NULL)
  108.         quit(MEMORY_ERR, "");
  109.  
  110.     lineSrcList = loadLines(argv[1], EXT_LINE1);
  111.     lineDstList = loadLines(argv[1], EXT_LINE2);
  112.     if (lineSrcList->number != 0) {
  113.         if (lineAsk(argv[1]) == 'N') {
  114.             createLines(Src, lineSrcList);
  115.             lineDstList->number = 0;
  116.         }
  117.         else 
  118.             editLines(Src, lineSrcList);
  119.     }
  120.     else
  121.         createLines(Src, lineSrcList);
  122.         
  123.     TargFlag = 1;    /* For the screen intro message */
  124.     NumLines = lineSrcList->number;
  125.     if (NumLines) {
  126.         /* Make sure the number of lines match */
  127.         if (lineDstList->number !=  NumLines) {    
  128.             /* didn't match, so copy the source lines */
  129.             lineDstList->number = NumLines;
  130.             for (segment = 0; segment < NumLines; segment++) 
  131.                 lineDstList->line[segment]
  132.                         = lineSrcList->line[segment];
  133.         }
  134.         editLines(Src, lineDstList);
  135.         saveLines(argv[1], lineSrcList, EXT_LINE1);
  136.         saveLines(argv[1], lineDstList, EXT_LINE2);
  137.         beep();
  138.  
  139.         for (segment = 0; segment < NumLines; segment++) {
  140.             DstLine[segment].p[0]=lineDstList->line[segment].p[0];
  141.             DstLine[segment].p[1]=lineDstList->line[segment].p[1];
  142.             setLength(&DstLine[segment]);
  143.             SrcLine[segment].p[0]=lineSrcList->line[segment].p[0];
  144.             SrcLine[segment].p[1]=lineSrcList->line[segment].p[1];
  145.             setLength(&SrcLine[segment]);
  146.         }
  147.         tweenWarp(Src);
  148.     }
  149.  
  150.     setTextMode();
  151. }
  152.  
  153. /*****************************************************************
  154. * FUNC: int    tweenWarp(PICTURE *src)
  155. * DESC: In-between the warping lines to the final warped image.
  156. *        Display the warped image as you go.
  157. *****************************************************************/
  158.  
  159. int
  160. tweenWarp(PICTURE *src)
  161. {
  162.     POINT warp;
  163.     COLOR scolor;
  164.     LINE warpLine[MAX_LINES];
  165.     int tween, line, p;
  166.  
  167.     setGraphicsMode();
  168.     displayPicture(src);
  169.     
  170.     if (Tweens > 1) {
  171.         saveScreen(&src->pal);
  172.         Tweens--;
  173.     }
  174.     /* src is already on screen, now tween to the target */
  175.     for (tween = 1; tween <= Tweens; tween++) {
  176.         /* Tween the control lines used to warp the images */
  177.         for (line = 0; line < NumLines; line++) {
  178.             /* tween each point of each line */
  179.             for (p = 0; p < 2; p++) {
  180.                 warpLine[line].p[p].x = SrcLine[line].p[p].x +
  181.                         ((DstLine[line].p[p].x
  182.                         - SrcLine[line].p[p].x) * tween) /Tweens;
  183.                 warpLine[line].p[p].y = SrcLine[line].p[p].y +
  184.                         ((DstLine[line].p[p].y
  185.                         - SrcLine[line].p[p].y) * tween) /Tweens;
  186.             }
  187.             setLength(&warpLine[line]);
  188.         }
  189.         /* Go through the screen and get warped source pixels */
  190.         for (warp.y = Ymin; warp.y <= Ymax; warp.y++)    {
  191.             for (warp.x = Xmin; warp.x <= Xmax; warp.x++)    {    
  192.                 _setcolor (sumLines(src, &scolor, SrcLine,
  193.                                             &warp, warpLine));
  194.                 _setpixel (warp.x, warp.y);
  195.             }
  196.         }
  197.         if (!OutFilename) {    /* no output file name */
  198.             beep();
  199.             waitForKey();    /* so pause to enjoy the pictures */
  200.         }
  201.         else
  202.             saveScreen(&src->pal);
  203.     }
  204. }
  205.  
  206.  
  207.